﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace xfsale.lib
{
    public static class Log
    {
        public const int LOGTO_TUXEDO = 0;
        public const int LOGTO_STDOUT = 1;
        public const int LOGTO_STDERR = 2;
        public const int LOGTO_FILE = 3;

        public const int LOGLVL_VERBOSE = 0;
        public const int LOGLVL_DEBUG = 1;
        public const int LOGLVL_INFO = 2;
        public const int LOGLVL_WARN = 3;
        public const int LOGLVL_ERROR = 4;

        private static int _logTo = LOGTO_FILE;
        private static int _logLevel = LOGLVL_VERBOSE;
        private static string _logFile = string.Empty;
        private static bool _logReset = false;

        public static int LogTo
        {
            get { return Log._logTo; }
            set { Log._logTo = value; }
        }

        public static int LogLevel
        {
            get { return Log._logLevel; }
            set { Log._logLevel = value; }
        }

        public static string LogFile
        {
            get { return Log._logFile; }
            set { Log._logFile = value; }
        }

        public static bool LogReset
        {
            get { return Log._logReset; }
            set { Log._logReset = value; }
        }

        public static void Verbose(string fmt, params object[] args)
        {
            _Log(LOGLVL_VERBOSE, LogTo, LogFile, string.Format(fmt, args));
        }

        public static void Debug(string fmt, params object[] args)
        {
            _Log(LOGLVL_DEBUG, LogTo, LogFile, string.Format(fmt, args));
        }

        public static void Info(string fmt, params object[] args)
        {
            _Log(LOGLVL_INFO, LogTo, LogFile, string.Format(fmt, args));
        }

        public static void Warn(string fmt, params object[] args)
        {
            _Log(LOGLVL_WARN, LogTo, LogFile, string.Format(fmt, args));
        }

        public static void Error(string fmt, params object[] args)
        {
            _Log(LOGLVL_ERROR, LogTo, LogFile, string.Format(fmt, args));
        }

        public static void ThrowError(string fmt, params object[] args)
        {
            string errMsg = string.Format(fmt, args);
            _Log(LOGLVL_ERROR, LogTo, LogFile, errMsg);
            throw new Error(errMsg);
        }

        //----------------------------------------------
        //	写系统日志
        //----------------------------------------------
        private static void _Log(int level, int to, string file, string str)
        {
            StreamWriter writer = null;

            if (LogLevel > level)
                return;

            switch (to)
            {
                case LOGTO_TUXEDO:
                    break;

                case LOGTO_STDOUT:
                    Console.Out.WriteLine(str);
                    break;

                case LOGTO_STDERR:
                    Console.Error.WriteLine(str);
                    break;

                case LOGTO_FILE:
                    try
                    {
                        writer = new StreamWriter(_logFile, true);
                        writer.WriteLine(str);
                    }
                    catch
                    {
                        Log.ThrowError("写入日志文件失败");
                    }
                    finally
                    {
                        writer.Close();
                    }
                    break;

                default:
                    Log.ThrowError("不支持的日志输出选项");
                    break;
            }
        }
    }
}
